package com.prj.ufdm.file.dao._impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsOperations;
import org.springframework.stereotype.Repository;
import org.springframework.web.multipart.MultipartFile;
import com.mongodb.DBObject;
import com.mongodb.client.gridfs.model.GridFSFile;
import com.prj.ufdm.file.dao.AloneFileGridFsDao;
import com.prj.ufdm.file.dao.WholeFileDao;
import com.prj.ufdm.file.model.ChunkFile;
import com.prj.ufdm.file.model.WholeFile;

@Repository
public class AloneFileGridFsDaoImpl implements AloneFileGridFsDao {

	@Autowired
	private GridFsOperations gridFsOperations;
	@Autowired
	private WholeFileDao wholeFileDao;
	/**
	 * DESC: 保存上传文件信息
	 * @param file
	 * @param metaData
	 * @return
	 * @throws Exception
	 */
	public GridFSFile storeGridFs(MultipartFile file,DBObject metaData) throws IOException {
		String fileId = gridFsOperations.store(file.getInputStream(), file.getOriginalFilename(),file.getContentType(),metaData).toString();
		GridFSFile gridFSFile = gridFsOperations.findOne(new Query(Criteria.where("_id").is(new ObjectId(fileId))));
		return gridFSFile;
	}

	/**
	 * 根据元文件的MD5查询所有的切片文件
	 * @param metaMd5
	 * @return
	 * @throws Exception
	 */
	public GridFSFile findGridFsByFileHash(String fileHash) throws Exception{
		Query query = new Query().
				addCriteria(Criteria.where("metadata.fileHash").is(fileHash));
		GridFSFile file = gridFsOperations.findOne(query);
		return file;
	}

	/**
	 * Desc: 根据 wholeFileHash 获取所有 GridFSFile 
	 * @param wholeFileHash
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<GridFSFile> findAllGridFsByWholeFileHash(String wholeFileHash) throws Exception {
		List<GridFSFile> gridFSFileList = new ArrayList<GridFSFile>();
		WholeFile wholeFile = wholeFileDao.findWholeFileByWholeFileHash(wholeFileHash);
		if(wholeFile!=null && wholeFile.getId()!=null && !wholeFile.getId().equals("")) {
			List<ChunkFile> listChunkFile = wholeFile.getListChunkFile();
			if(listChunkFile!=null && listChunkFile.size()>0) {
				System.out.println("排序前："+listChunkFile);
				// 根据 chunkNum 排序
				listChunkFile.sort(new Comparator<ChunkFile>() {
					@Override
					public int compare(ChunkFile obj1, ChunkFile obj2) {
						return Integer.parseInt(obj1.getChunkNum()) - Integer.parseInt(obj2.getChunkNum());
					}
				});
				System.out.println("排序后："+listChunkFile);
				for(ChunkFile chunkFile: listChunkFile) {
					GridFSFile gridFSFile = gridFsOperations.findOne(new Query(Criteria.where("_id").is(new ObjectId(chunkFile.getFileId()))));
					gridFSFileList.add(gridFSFile);
				}
			}
		}
		return gridFSFileList;
	}
	
	/**
	 * Desc: 根据wholeFileId 获取所有 GridFSFile 
	 * @param wholeFileId
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<GridFSFile> findAllGridFsByWholeFileId(String wholeFileId) throws Exception{
		List<GridFSFile> gridFSFileList = new ArrayList<GridFSFile>();
		WholeFile wholeFile = wholeFileDao.findWholeFileByWholeFileId(wholeFileId);
		List<ChunkFile> listChunkFile = wholeFile.getListChunkFile();
		
		if(listChunkFile!=null && listChunkFile.size()>0) {
			System.out.println("排序前："+listChunkFile);
			// 根据 chunkNum 排序
			listChunkFile.sort(new Comparator<ChunkFile>() {
				@Override
				public int compare(ChunkFile obj1, ChunkFile obj2) {
					return Integer.parseInt(obj1.getChunkNum()) - Integer.parseInt(obj2.getChunkNum());
				}
			});
			System.out.println("排序后："+listChunkFile);
			
			for(ChunkFile chunkFile: listChunkFile) {
				GridFSFile gridFSFile = gridFsOperations.findOne(new Query(Criteria.where("_id").is(new ObjectId(chunkFile.getFileId()))));
				gridFSFileList.add(gridFSFile);
			}
		}
		return gridFSFileList;
	}
	
}
